/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client.section;

import org.ajiaojr.enigmagm.client.SortablePaged;
import org.ajiaojr.enigmagm.client.dialog.CharacterDetailsDialog;
import org.ajiaojr.enigmagm.client.service.CharacterService;
import org.ajiaojr.enigmagm.client.service.CharacterServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SourcesTableEvents;
import com.google.gwt.user.client.ui.TableListener;
import com.google.gwt.user.client.ui.VerticalPanel;

import java.util.List;

/**
 * Section that displays the guild roster.
 * 
 * @author ajiaojr
 */
public class GuildRosterSection extends Section {
  private class CharactersGrid extends SortablePaged {
    public static final int MAX_RESULTS = 30;

    public CharactersGrid(Panel panel) {
      super(panel, MAX_RESULTS, "character_name");
    }

    public void populate() {
      CharacterServiceAsync characterService =
          CharacterService.Util.getInstance();
      characterService.list(sortAttribute, ascending, firstResult, maxResults,
          new AsyncCallback() {

            public void onFailure(Throwable caught) {
              Window.alert(caught.getMessage());
            }

            public void onSuccess(Object result) {
              panel.clear();

              final List characters = (List) result;

              Grid grid = new Grid(characters.size() + 1, 5);
              grid.setWidth("100%");
              grid.setStyleName("gwt-table");
              grid.addStyleName("dataTable");
              grid.setText(0, 0, messages.charactersGrid_characterName());
              grid.setText(0, 1, messages.charactersGrid_level());
              grid.setText(0, 2, messages.charactersGrid_raceName());
              grid.setText(0, 3, messages.charactersGrid_className());
              grid.setText(0, 4, messages.charactersGrid_rankName());
              grid.getRowFormatter().addStyleName(0, "header");
              grid.getColumnFormatter().setWidth(0, "130px");
              grid.getColumnFormatter().setWidth(1, "60px");
              grid.getColumnFormatter().setWidth(2, "100px");
              grid.getColumnFormatter().setWidth(3, "100px");
              grid.addTableListener(new TableListener() {

                public void onCellClicked(SourcesTableEvents sender, int row,
                    int cell) {
                  if (row == 0) {
                    switch (cell) {
                      case 0:
                        setSortAttribute("character_name");
                        break;
                      case 1:
                        setSortAttribute("level");
                        break;
                      case 2:
                        setSortAttribute("race_name");
                        break;
                      case 3:
                        setSortAttribute("class_name");
                        break;
                      case 4:
                        setSortAttribute("rank_id");
                        break;
                    }
                  } else {
                    org.ajiaojr.enigmagm.client.data.Character r =
                        (org.ajiaojr.enigmagm.client.data.Character) characters
                            .get(row - 1);
                    CharacterDetailsDialog rdd = new CharacterDetailsDialog(r);
                    rdd.show();
                  }
                }
              });
              grid.getRowFormatter().addStyleName(0, "clickable");

              for (int i = 0; i < characters.size(); i++) {
                org.ajiaojr.enigmagm.client.data.Character c =
                    (org.ajiaojr.enigmagm.client.data.Character) characters
                        .get(i);
                int currentRow = i + 1;

                grid.setText(currentRow, 0, c.getCharacterName());
                grid.setText(currentRow, 1, Integer.toString(c.getLevel()));
                grid.setText(currentRow, 2, c.getRace());
                grid.setText(currentRow, 3, c.getCharacterClass());
                grid.setText(currentRow, 4, c.getRank());
                grid.getRowFormatter().addStyleName(currentRow, "clickable");
                grid.getRowFormatter().addStyleName(currentRow, "highlight");
              }
              panel.add(grid);
              attachNavBar();
            }
          });
    }
  }

  private static GuildRosterSection instance = new GuildRosterSection();

  public static GuildRosterSection getInstance() {
    return instance;
  }

  private GuildRosterSectionMessages messages =
      (GuildRosterSectionMessages) GWT.create(GuildRosterSectionMessages.class);

  private GuildRosterSection() {
    final HorizontalPanel mainPanel = new HorizontalPanel();
    initWidget(mainPanel);
    mainPanel.setWidth("100%");

    final VerticalPanel verticalPanel = new VerticalPanel();
    mainPanel.add(verticalPanel);
    mainPanel.setCellVerticalAlignment(verticalPanel,
        HasVerticalAlignment.ALIGN_TOP);
    verticalPanel.setWidth("100%");
    verticalPanel.setSpacing(6);

    Frame adFrame = new Frame(GWT.getModuleBaseURL() + "/ad.html");
    adFrame.setStyleName("adBox");

    mainPanel.add(adFrame);
    adFrame.setSize("200px", "350px");
    mainPanel.setCellVerticalAlignment(adFrame, HasVerticalAlignment.ALIGN_TOP);
    mainPanel.setCellHorizontalAlignment(adFrame,
        HasHorizontalAlignment.ALIGN_CENTER);
    mainPanel.setCellWidth(adFrame, "140px");

    final VerticalPanel verticalPanel_1 = new VerticalPanel();
    verticalPanel.add(verticalPanel_1);
    verticalPanel_1.setWidth("100%");

    CharactersGrid cg = new CharactersGrid(verticalPanel_1);
    cg.populate();

    final Label tipLabel =
        new Label(messages.section_tip(messages.section_tip()));
    verticalPanel.add(tipLabel);
    tipLabel.setStyleName("tip");
    tipLabel.setWidth("100%");
  }

  public String getName() {
    return messages.section_name();
  }
}
